package com.example.utils;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.example.entity.QuestionDetail;
import com.google.gson.Gson;
import okhttp3.*;

import java.io.IOException;

public class QuestionDetailUtil {


    public static void main(String[] args) {
        getQuestionById(1897140814803488769L);
    }

    public static QuestionDetail getQuestionById(Long questionId) {
        // 创建 OkHttpClient 实例
        OkHttpClient client = new OkHttpClient();

        // 创建请求对象
        Request request = new Request.Builder()
                .url("http://localhost:9999/system/question/detail?questionId="+questionId) // 替换为你要请求的 URL
                .addHeader("Authorization","eyJhbGciOiJIUzUxMiJ9.eyJ1c2VySWQiOjEsInVzZXJLZXkiOiI2NTNlYjE3OS1iYTA5LTRiOWUtYmEyYy0yNGM5MzYzOTZjZjQifQ.RlX1SEFpNFPoS8azz93Gq9LmAlDF6o-BVm3Ra56MJmfm_njjzlIu6In11vuJplOccy4xyqsZmlNcajf8uvYPFg")
                .build();
        QuestionDetail questionDetail = null;
        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                // 获取响应体
                String jsonStr = response.body().string();
                // 解析 JSON 字符串
                JSONObject jsonObject = JSONObject.parseObject(jsonStr);
                // 获取 choices 数组
                JSONObject data = jsonObject.getJSONObject("data");
                if (data != null && !data.isEmpty()) {
                    Gson gson = new Gson();
                    questionDetail = gson.fromJson(String.valueOf(data), QuestionDetail.class);
                    System.out.println("questionDetail: "+questionDetail.toString());
                }


            } else {
                System.out.println("请求失败，响应码: " + response.code());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return questionDetail;
    }


}
